<?php

function financeaccount_del()
{
    $id = intval($_GET['id']);
    $rs = getdb("select account from k_user where acl=6 and id=" . $id);
    if ($rs->eof)
    {
        echo "<p class=error>错误的帐号 ID!</p>";
        exit();
    }
    $account = $rs->f['account'];
    $rs->close();
    if (isset($_GET['ok']))
    {
        getdb("delete from k_user where account='{$account}'");
        echo "<p>删除帐号成功！</p>";
        exit();
    }
    else
    {
        echo "<p>是否确定删除这个财务帐号？<b>" . $account . "</b></p>";
        echo "<p>[ &nbsp; ";
        echo "<a href=op.php?op=admin&fp=financeaccount&gametype=" . $gametype . "&cmd=del&id=" . $id . "&ok=1>是</a>";
        echo " &nbsp; | &nbsp; <a href=op.php?op=admin&fp=financeaccount&gametype=" . $gametype . ">否</a> &nbsp; ]</p>";
    }
}

function financeaccount_list()
{
    global $curruser;
    global $gametype;
    echo "<table border=1 cellpadding=3 cellspacing=0>";
    echo "<tr class='td0 bgClc_{$gametype}'><td nowrap>帐号</td><td nowrap>名称</td><td nowrap align=center>注册时间</td><td nowrap align=center>登陆时间</td><td nowrap align=center>登陆IP</td><td nowrap align=center>状态</td><td nowrap align=center>功能</td>";
    echo "</tr>";
    $rs = getdb("select * from k_user where acl=6");
    while (!$rs->eof)
    {
        echo "<tr>";
        echo "<td nowrap>" . $rs->f['account'] . "</td>";
        echo "<td nowrap>" . $rs->f['name'] . "</td>";
        echo "<td align=center>" . date('Y-m-d H:i:s', $rs->f['createdt']) . "</td>";
        echo "<td align=center>";
        if (!empty($rs->f['lastlogdt']))
        {
            echo date('Y-m-d H:i:s', $rs->f['lastlogdt']);
        }
        echo "</td>";
        echo "<td align=center>" . $rs->f['lastlogip'] . "</td>";
        echo "<td align=center>" . ($rs->f['enabled'] == 1 ? "开启" : "停用") . "</td>";
        echo "<td align=center>";
        echo " [ <a href=op.php?op=admin&fp=financeaccount&gametype=" . $gametype . "&cmd=edit&id=" . $rs->f['id'] . ">修改</a>";
        echo " | <a href=op.php?op=admin&fp=financeaccount&gametype=" . $gametype . "&cmd=del&id=" . $rs->f['id'] . ">删除</a>";
        echo " ] </td>";
        echo "</tr>";
        $rs->movenext();
    }
    echo "</table>";
}

function financeaccount_add()
{
    global $curruser;
    global $gametype;
    $err = "";
    if (isset($_POST['account']))
    {
        $account = getvar("account");
        $pass = getvar("pass");
        if ($account == "")
        {
            $err .= "请输入帐号！\\n";
        }
        if ($pass == "")
        {
            $err .= "请输入密码！\\n";
        }
        if ($err == "")
        {
            $rs = getdb("select * from k_user where account='" . $account . "'");
            if (!$rs->eof)
            {
                $err .= "帐号已经存在，请用其他的帐号！\\n";
            }
        }
        if ($err == "")
        {
            if (!check_account_legal($account))
            {
                $err .= "帐号必须为６个以上的英文字母及数字混和所组成！（请注意英文大小写）";
            }
            if (!check_account_legal($pass) && $err == "")
            {
                $err .= "密码必须为６个以上的英文字母及数字混和所组成！（请注意英文大小写）";
            }
            $field_chk = db_IsExistsField("k_user", "createAccount");
            if ($field_chk === FALSE)
            {
                $err = "资料表异常,请洽系统管理员";
            }
            else if ($field_chk == 0)
            {
                getdb("ALTER TABLE `k_user` ADD `createAccount` VARCHAR( 20 ) NULL ");
            }
            if ($err == "")
            {
                $name = getvar("name");
                if ($name == "")
                {
                    $name = $account;
                }
                $sql = "insert into k_user (account,name,pass,acl,enabled,prev_id,createdt,createAccount) values (" . sqlstr($account) . "," . sqlstr($name) . ",'" . md5($pass) . "',6,1," . $curruser['id'] . "," . time() . ",'" . $curruser['account'] . "')";
                getdb($sql);
                echo "<p>新增帐号成功！</p>";
                echo "<p>帐号：<b>{$account}</b><br>";
                echo "密码：<b>{$pass}</b></p>";
                return;
            }
        }
    }
    echo "<form action=\"op.php?op=admin&fp=financeaccount&gametype=" . $gametype . "&cmd=add\" method=post onsubmit=\"if (this.account.value=='') {\n\talert('请输入帐号！');\n\tthis.account.focus();\n\treturn false;\n\t}\n\tif (this.pass.value=='') {\n\talert('请输入密码！');\n\tthis.pass.focus();\n\treturn false;\n\t}\n\tif (this.pass.value != this.pass2.value) {\n\talert('两个密码不一样，请检查！');\n\tthis.pass.focus();\n\treturn false;\n\t}\">";
    echo "<table border=0 cellpadding=2 cellspacing=0>";
    echo "<tr><td align=right>帐号：</td><td><input name=account size=20 maxlength=25> (最少6码)</td></tr>";
    echo "<tr><td align=right>名称：</td><td><input name=name size=20 maxlength=25></td></tr>";
    echo "<tr><td align=right>密码：</td><td><input type=password name=pass size=20 maxlength=25> (最少6码)</td></tr>";
    echo "<tr><td align=right>再次输入密码：</td><td><input type=password name=pass2 size=20 maxlength=25></td></tr>";
    echo "</table>";
    echo "<input type=submit value=新增>";
    echo "</form>";
    if ($err != "")
    {
        echo "<script>alert(\"" . $err . "\");</script>";
    }
}

function financeaccount_edit()
{
    $id = intval($_GET['id']);
    $rs = getdb("select * from k_user where acl=6 and id=" . $id);
    if ($rs->eof)
    {
        echo "<p class=error>错误的帐号 ID!</p>";
        exit();
    }
    if (ispost())
    {
        $f = array();
        if (getvar("pass") != "")
        {
            if (!check_account_legal(getvar("pass")))
            {
                echo "<p class=error>密码必须为６个以上的英文字母及数字混和所组成！（请注意英文大小写）</p>";
                exit();
            }
            else
            {
                $f['pass'] = "'" . md5(getvar("pass")) . "'";
            }
        }
        $f['enabled'] = getvar("enabled") == 1 || getvar("enabled") == 0 ? getvar("enabled") : 0;
        $f['name'] = "'" . getvar("name") . "'";
        db_update("k_user", $f, "acl=6 and id=" . $id);
        echo "<p>资料修改成功！</p>";
        exit();
    }
    else
    {
        echo "<form action=\"op.php?op=admin&fp=financeaccount&gametype=" . $gametype . "&cmd=edit&id=" . $id . "\" method=post onsubmit=\"if (this.pass.value!='') {\r\n\tif (this.pass.value != this.pass2.value) {\r\n\talert('两个密码不一样，请检查！');\r\n\tthis.pass.focus();\r\n\treturn false;\r\n\t}\r\n\t}\">";
        echo "<table border=0 cellpadding=2 cellspacing=0>";
        echo "<tr><td align=right>帐号：</td><td>" . $rs->f['account'] . "</td></tr>";
        echo "<tr><td align=right>名称：</td><td><input name=name value=\"" . $rs->f['name'] . "\" size=20 maxlength=25></td></tr>";
        echo "<tr><td align=right>密码：</td><td><input type=password name=pass size=20 maxlength=25> (最少6码)</td></tr>";
        echo "<tr><td align=right>再次输入密码：</td><td><input type=password name=pass2 size=20 maxlength=25></td></tr>";
        echo "<tr><td align=right>状态：</td><td><input type=\"radio\" name=\"enabled\" value=\"1\"" . ($rs->f['enabled'] == 1 ? "checked=checked" : "") . "/>开启 <input type=\"radio\" name=\"enabled\" value=\"0\"" . ($rs->f['enabled'] == 0 ? "checked=checked" : "") . "/>停用</td></tr>";
        echo "</table>";
        echo "<input type=submit value=修改>";
        echo "</form>";
    }
}

$cmd = "list";
if (isset($_GET['cmd']))
{
    $cmd = $_GET['cmd'];
}
$gametype = intval(getvar("gametype"));
$gType = chkGametype($gametype);
if ($gType == 0)
{
    $ui = new UI();
}
else if (isset($g_menu_enabled[$gametype]) && $g_menu_enabled[$gametype])
{
    include_once( "class_3d/CheckMode_3d.php" );
    $chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
    $chk->checkShow3dPage();
    unset($chk);
    $ui = new UI_3D();
    $jslink = "<script language=\"JScript.Encode\" src=\"site.js\"></script>" . "<script language=\"VBScript.Encode\" src=\"site.vbs\"></script>";
    $ui->set("jslink", $jslink);
    $ui->header();
}
else
{
    exit("error gametype");
}
echo title("财务∣出纳帐号管理", "<a href=op.php?op=admin&fp=financeaccount&gametype=" . $gametype . "&cmd=list>列表</a>" . " | <a href=op.php?op=admin&fp=financeaccount&gametype=" . $gametype . "&cmd=add>新增</a>");
echo "<p>";
switch ($cmd)
{
    case "edit" :
        financeaccount_edit();
        break;
    case "add" :
        financeaccount_add();
        break;
    case "del" :
        financeaccount_del();
        break;
    case "list" :
        financeaccount_list();
        break;
}
$ui->footer();